using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.ChatRoom;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.Page
{
	public class CardConfirm : BasePage
	{
		private const string TAG = "CardConfirm";
		private ApplicationBarIconButton okBarBtn;
		private string cuUsername;
		private Contact cuCardContact;
		private ChatPageCtrl cufromPage;
		private List<Contact> conList;
		private SelectCard.SelectCardFromType fromType;
		private EventWatcher mCreateWatcher;
		private List<string> validList;
		private bool isShowLoading;
		internal Grid LayoutRoot;
		internal TextBlock TitleTxt;
		internal StackPanel PeoplePanel;
		internal ShareCardShow ShareCardPeople;
		internal TextBlock ShareCardDescTxt;
		internal ScrollViewer sharePeopleListScrollViewer;
		internal PeopleList ToPeopleList;
		internal ApplicationBarIconButton OkBtn;
		private bool _contentLoaded;
		public CardConfirm()
		{
			this.InitializeComponent();
			if (base.get_ApplicationBar() != null && base.get_ApplicationBar().get_Buttons() != null && base.get_ApplicationBar().get_Buttons().get_Count() > 0)
			{
				this.okBarBtn = (base.get_ApplicationBar().get_Buttons().get_Item(0) as ApplicationBarIconButton);
				if (this.okBarBtn != null)
				{
					this.okBarBtn.set_Text(strings.COM_Confirm);
				}
			}
			this.ToPeopleList.NowListMode = PeopleList.PeopleListMode.CommonModeMode;
			this.ToPeopleList.ShowType = 1;
			this.ToPeopleList.LineNum = 1;
			this.ToPeopleList.VerSpace = 25;
			this.ToPeopleList.InvalidateMeasure();
			this.ToPeopleList.InvalidateArrange();
			if (R.isEnglish())
			{
				this.TitleTxt.set_Text("share name card");
			}
		}
		public static void StartCardConfirm(BasePage originPage, string toUsername, Contact cardContact, ChatPageCtrl fromPage)
		{
			originPage.GoToPage("/Source/UI/Chat/Card/CardConfirm.xaml", new object[]
			{
				toUsername,
				cardContact,
				fromPage,
				SelectCard.SelectCardFromType.fromChatPage
			}, false);
		}
		public static void StartCardConfirm(BasePage originPage, List<Contact> contactList, Contact cardContact, ChatPageCtrl fromPage)
		{
			originPage.GoToPage("/Source/UI/Chat/Card/CardConfirm.xaml", new object[]
			{
				contactList,
				cardContact,
				fromPage,
				SelectCard.SelectCardFromType.fromProfilePage
			}, false);
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && Enumerable.Count<object>(object_0) == 4)
			{
				this.cuCardContact = (object_0[1] as Contact);
				this.cufromPage = (object_0[2] as ChatPageCtrl);
				this.fromType = (SelectCard.SelectCardFromType)object_0[3];
				if (this.fromType == SelectCard.SelectCardFromType.fromChatPage)
				{
					this.cuUsername = (object_0[0] as string);
				}
				else
				{
					this.conList = (object_0[0] as List<Contact>);
				}
				this.InitUI();
			}
		}
		protected override void OnPageOut()
		{
		}
		private void InitUI()
		{
			if (this.fromType == SelectCard.SelectCardFromType.fromProfilePage && this.conList != null && this.conList.get_Count() > 1)
			{
				Log.smethod_5("CardConfirm", "It's a Chat room,getUserContactList start");
				using (List<Contact>.Enumerator enumerator = this.conList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Contact current = enumerator.get_Current();
						if (!(current.strUsrName == AccountMgr.getCurAccount().strUsrName))
						{
							string contactDisplayName = ContactHelper.GetContactDisplayName(current);
							this.ToPeopleList.AddPeople(current.strUsrName, contactDisplayName);
						}
					}
					goto IL_10E;
				}
			}
			Log.smethod_5("CardConfirm", "It's a single Chat");
			if (this.fromType == SelectCard.SelectCardFromType.fromChatPage)
			{
				if (string.IsNullOrEmpty(this.cuUsername))
				{
					return;
				}
				this.ToPeopleList.AddPeople(this.cuUsername, ContactHelper.getDisplayName(this.cuUsername, null));
			}
			else
			{
				this.ToPeopleList.AddPeople(this.conList.get_Item(0).strUsrName, ContactHelper.getDisplayName(this.conList.get_Item(0).strUsrName, null));
			}
			IL_10E:
			this.InitShareCard();
		}
		private void InitShareCard()
		{
			if (this.cuCardContact == null)
			{
				return;
			}
			ChatPeopleInfo chatPeopleInfo = new ChatPeopleInfo();
			chatPeopleInfo.UserName = this.cuCardContact.strUsrName;
			chatPeopleInfo.NickName = ContactHelper.GetContactDisplayName(this.cuCardContact);
			chatPeopleInfo.Alias = this.cuCardContact.strAlias;
			this.ShareCardPeople.People = chatPeopleInfo;
			if (this.fromType == SelectCard.SelectCardFromType.fromProfilePage)
			{
				this.sharePeopleListScrollViewer.set_Visibility(0);
				this.ShareCardDescTxt.set_Text(strings.Message_ShareCard_To);
				return;
			}
			this.sharePeopleListScrollViewer.set_Visibility(1);
			this.ShareCardDescTxt.set_Text(strings.Contacts_Confirm_ToThoseReceiverFormat);
		}
		private void OkBtn_Click(object sender, EventArgs e)
		{
			if (this.cuCardContact == null)
			{
				return;
			}
			if (this.cufromPage == null)
			{
				return;
			}
			if (this.fromType == SelectCard.SelectCardFromType.fromChatPage)
			{
				this.cufromPage.SendShareCard(this.cuCardContact);
			}
			else
			{
				if (this.fromType == SelectCard.SelectCardFromType.fromProfilePage)
				{
					if (this.conList == null || this.conList.get_Count() != 1)
					{
						this.CreateChatRoomInit();
						this.ShowLoading(strings.ChatCreateWaitInfo);
						List<string> list = new List<string>();
						using (List<Contact>.Enumerator enumerator = this.conList.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								Contact current = enumerator.get_Current();
								list.Add(current.strUsrName);
							}
						}
						ServiceCenter.sceneCreateChatRoomService.doScene(strings.ChatDefaultTopic, list);
						return;
					}
					this.cufromPage.cuUser = this.conList.get_Item(0).strUsrName;
					this.cufromPage.SendShareCard(this.cuCardContact);
					base.get_Dispatcher().BeginInvoke(delegate
					{
						ChatContainerPage.GoToChatPage(this, this.conList.get_Item(0).strUsrName, null, null, null);
					});
				}
			}
			while (PageManager.getInstance().GetPageCount() > 1)
			{
				PageManager.getInstance().PopPage();
			}
			MainPage.mainPageInstance.ChangeToSessionView();
			base.GoBack();
		}
		private void CreateChatRoomInit()
		{
			Log.smethod_5("CardConfirm", "registerEventWatcher CreateFromMainPageInit");
			this.mCreateWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onChatRoomEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_SUCCESS, this.mCreateWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_ERR, this.mCreateWatcher);
		}
		private void CreateChatRoomUnint()
		{
			Log.smethod_5("CardConfirm", "removeEventWatcher CreateFromMainPageUnint");
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_SUCCESS, this.mCreateWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_ERR, this.mCreateWatcher);
			this.mCreateWatcher = null;
		}
		private void onChatRoomEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			Log.smethod_5("CardConfirm", "onChatRoomEventHandler:" + evtArgs.mEventID);
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_NETSCENE_CREATE_CHATROOM_SUCCESS:
			{
				NetSceneCreateChatRoomResult netSceneCreateChatRoomResult = evtArgs.mObject as NetSceneCreateChatRoomResult;
				if (netSceneCreateChatRoomResult == null)
				{
					Log.smethod_1("CardConfirm", "netSceneCreateChatRoomRet == null");
					return;
				}
				if (netSceneCreateChatRoomResult.sceneCreateChatRoom == null)
				{
					Log.smethod_1("CardConfirm", "netSceneCreateChatRoomRet.sceneCreateChatRoom == null");
					return;
				}
				if (string.IsNullOrEmpty(netSceneCreateChatRoomResult.sceneCreateChatRoom.chatRoomName))
				{
					Log.smethod_1("CardConfirm", "sceneCreateChatRoom.chatRoomName == null");
					return;
				}
				this.cufromPage.cuUser = netSceneCreateChatRoomResult.sceneCreateChatRoom.chatRoomName;
				this.cufromPage.SendShareCard(this.cuCardContact);
				base.get_Dispatcher().BeginInvoke(delegate
				{
					ChatContainerPage.GoToChatPage(this, this.cufromPage.cuUser, null, null, null);
				});
				this.HideLoading();
				while (PageManager.getInstance().GetPageCount() > 1)
				{
					PageManager.getInstance().PopPage();
				}
				MainPage.mainPageInstance.ChangeToSessionView();
				base.GoBack();
				return;
			}
			case EventConst.ON_NETSCENE_CREATE_CHATROOM_ERR:
			{
				NetSceneCreateChatRoomResult netSceneCreateChatRoomResult2 = evtArgs.mObject as NetSceneCreateChatRoomResult;
				if (netSceneCreateChatRoomResult2 == null)
				{
					MMMessageBox.Show(strings.ChatCreateErrorInfo, true, "确定");
					Log.smethod_1("CardConfirm", "createChatRoomRet == null");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom == null)
				{
					MMMessageBox.Show(strings.ChatCreateErrorInfo, true, "确定");
					Log.smethod_1("CardConfirm", "netSceneCreateChatRoomRet.sceneCreateChatRoom == null");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.invalidUsernameList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.invalidUsernameList.get_Count() != 0)
				{
					MMMessageBox.Show(this.getShowInfo(netSceneCreateChatRoomResult2.sceneCreateChatRoom.invalidUsernameList, strings.ChatUserNameError), true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.notExistUserList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.notExistUserList.get_Count() != 0)
				{
					MMMessageBox.Show(this.getShowInfo(netSceneCreateChatRoomResult2.sceneCreateChatRoom.notExistUserList, strings.ChatUserNameNoExist), true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.memeberBlackList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.memeberBlackList.get_Count() != 0)
				{
					MMMessageBox.Show(this.getShowInfo(netSceneCreateChatRoomResult2.sceneCreateChatRoom.memeberBlackList, strings.ChatRefruseToAdd), true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.verifyUserList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.verifyUserList.get_Count() != 0)
				{
					this.HideLoading();
					this.ValidToFriend();
					return;
				}
				if (netSceneCreateChatRoomResult2.retCode == RetConst.MM_ERR_CLIENT)
				{
					this.HideLoading();
					MMMessageBox.Show(strings.ChatCreateNetError, true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.retCode == RetConst.MM_ERR_MEMBER_TOOMUCH)
				{
					this.HideLoading();
					MMMessageBox.Show(strings.ChatCreateOverMaxNum, true, "确定");
					return;
				}
				MMMessageBox.Show(strings.ChatCreateErrorInfo, true, "确定");
				return;
			}
			default:
				return;
			}
		}
		private string getShowInfo(List<string> userlist, string formatStr)
		{
			string text = "";
			if (userlist != null && userlist.get_Count() != 0)
			{
				for (int i = 0; i < userlist.get_Count(); i++)
				{
					Contact contact = StorageMgr.contact.method_0(userlist.get_Item(i));
					if (i == userlist.get_Count() - 1)
					{
						text += ((contact == null) ? userlist.get_Item(i) : ContactHelper.GetContactDisplayName(contact));
					}
					else
					{
						text = text + ((contact == null) ? userlist.get_Item(i) : ContactHelper.GetContactDisplayName(contact)) + ",";
					}
				}
			}
			return text + formatStr;
		}
		public void ValidToFriend()
		{
			if (this.validList != null && this.validList.get_Count() != 0)
			{
				Dialog2BtnWithInputBox.Show(strings.ChatNeedValidTitle, strings.ChatNeedValidInfo, strings.COM_Confirm, strings.COM_Cancel, "", new Dialog2BtnWithInputBox.dismissWithClickBtn(this.SayHiDialogHandler), null, null);
				return;
			}
		}
		private void SayHiDialogHandler(object sender, int nClickIndex)
		{
			if (this.validList != null && this.validList.get_Count() != 0)
			{
				if (nClickIndex == 0 && sender != null && sender is Dialog2BtnWithInputBox)
				{
					Dialog2BtnWithInputBox dialog2BtnWithInputBox = sender as Dialog2BtnWithInputBox;
					string text = dialog2BtnWithInputBox.txtIn.get_Text();
					if (text == null || text.get_Length() == 0)
					{
						text = strings.ChatDefaultHello;
					}
					ServiceCenter.sceneVerifyUser.doSceneSendMultiRequests(this.validList, AddContactScene.MM_ADDSCENE_CHATROOM, text);
					MessageBoxManager.ShowToast(strings.ChatHelloSuccess, 2500, null, false);
				}
				return;
			}
		}
		public void ShowLoading(string info)
		{
			if (this.isShowLoading)
			{
				return;
			}
			this.isShowLoading = true;
			ProcessingCover.getProgressBar(this.LayoutRoot, info, null, false, false, null);
		}
		public void HideLoading()
		{
			if (!this.isShowLoading)
			{
				return;
			}
			this.isShowLoading = false;
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/Card/CardConfirm.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.TitleTxt = (TextBlock)base.FindName("TitleTxt");
			this.PeoplePanel = (StackPanel)base.FindName("PeoplePanel");
			this.ShareCardPeople = (ShareCardShow)base.FindName("ShareCardPeople");
			this.ShareCardDescTxt = (TextBlock)base.FindName("ShareCardDescTxt");
			this.sharePeopleListScrollViewer = (ScrollViewer)base.FindName("sharePeopleListScrollViewer");
			this.ToPeopleList = (PeopleList)base.FindName("ToPeopleList");
			this.OkBtn = (ApplicationBarIconButton)base.FindName("OkBtn");
		}
	}
}
